<template> <!-- vue实例外创建 -->
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>{{ title }}</el-breadcrumb-item>
</el-breadcrumb>
    <el-card class="box-card">
      <el-button class="add" type="primary" @click="drawer = true">添加</el-button>
      <el-input placeholder="请输入内容" v-model="info.query" class="input-with-select">
        <el-button @click="getdata(1)" slot="append" icon="el-icon-search"></el-button>
      </el-input>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column type="index" label="#">
        </el-table-column>
        <el-table-column label="日期">
          <template slot-scope="scope">
            <span>{{ (scope.row.create_time * 1000) | newdate }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="mobile" label="mobile">
        </el-table-column>
        <el-table-column prop="role_name" label="真实姓名">
        </el-table-column>
        <el-table-column prop="email" label="地址">
        </el-table-column>
        <el-table-column prop="username" label="姓名">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[5, 10, 20, 50]"
        :current-page="info.pagenum" :page-size="info.pagesize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加区域 -->
    <el-drawer title="我是标题" :visible.sync="drawer" :direction="direction" :before-close="handleClose">
      <el-form :rules="rules" ref="userref" :model="userform" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userform.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="userform.password"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="userform.email"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="userform.mobile"></el-input>
        </el-form-item>
        <el-button class="add" type="primary" @click="adduserfun">确定</el-button>
      </el-form>
    </el-drawer>
    <!-- 修改区域 -->
    <el-dialog title="修改用户信息" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
      <el-form :rules="rules" ref="userref" :model="userform" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userform.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="userform.email"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="userform.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editfun">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { GetUsers, AddUsers, GetOneUsers, EditUsers,DelUsers } from '@/api/user';
export default {
  data() {
    return {
      info: {
        pagenum: 1,
        pagesize: 10,
        query: '',
      },
      tableData: [],
      // 添加区域
      drawer: false,
      direction: 'rtl',
      userform: {},
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "change" },],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      total: 0,
      dialogVisible: false,
      title:'',
    }
  },
  watch:{
    drawer(){
      this.userform = {}
    },
    dialogVisible(){
      this.userform = {}
    }
  },
  methods: {
    handleSizeChange(val) {
      this.info.pagesize = val
      this.info.pagenum = 1
      this.getdata()
    },
    handleCurrentChange(val) {
      this.info.pagenum = val
      this.getdata()
    },
    //修改 
    async handleEdit(index, { id }) {
      this.dialogVisible = true
      let { data, meta } = await GetOneUsers(id)
      if (meta.status != 200) return this.$message.error(meta.msg)
      this.userform = data
    },
    // 确定修改
    async editfun() {
      let { data, meta } = await EditUsers(this.userform)
      if (meta.status != 200) return this.$message.error(meta.msg)
      this.$message.success(meta.msg)
      // 1.关闭窗口
      this.dialogVisible = false
      // 2.更新页面
      this.getdata()
      // 3.清空数据
      this.userform = {}
    },
    // 删除
     async handleDelete(index, { id }) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then( async() => {
            let {data,meta} = await DelUsers(id)
            if (meta.status != 200) return this.$message.error(meta.msg)
          this.$message.success(meta.msg)
        this.getdata()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      console.log(id);
    },
    // 获取数据
    async getdata(e) {
      if (e == 1) {
        this.info.pagenum = 1
      }
      let { data, meta } = await GetUsers(this.info)
      if (meta.status != 200) return this.$message.error(meta.msg)
      this.tableData = data.users
      this.total = data.total
    },
    // 添加区域
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    // 确认添加
    adduserfun() {
      this.$refs['userref'].validate(async v => {
        if (v) {
          //发起添加请求
          let { data, meta } = await AddUsers(this.userform)
          if (meta.status != 201) return this.$message.error(meta.msg)
          this.$message.success(meta.msg)
          // 1.关闭窗口
          this.drawer = false
          // 2.更新页面
          this.getdata()
          // 3.清空数据
          this.userform = {}
          // this.$refs['userref'].resetFields();

        }
      })
    }
  },
  mounted() {
    this.getdata()
    this.title = localStorage.getItem('title')
  }

}
</script>
<style scoped>
.add {
  margin-bottom: 10px;
}

.el-form {
  text-align: center;
}

.input-with-select {
  width: 30%;
  margin-left: 15px;
}
</style>